#pragma once

#include <iostream>
#include <memory>
// #include<jsoncpp/json/json.h>

namespace Protocol
{
    // 1. 结构化数据的序列和反序列化
    // 2. 还要解决用户区分报文边界 --- 数据包粘报问题
    const std::string ProtSep = " ";
    const std::string LineBreakSep = "\n";
    // "len\nx op y\n" : \n不属于报文的一部分，约定
    std::string Encode(const std::string &message)
    {
        std::string len = std::to_string(message.size());
        std::string package = len + LineBreakSep + message + LineBreakSep;
        return package;
    }

    bool Decode(std::string &package, std::string *message)
    {
        auto pos = package.find(LineBreakSep);
        if (pos == std::string::npos)
        {
            return false;
        }
        std::string len = package.substr(0, pos);
        int total = len.size() + std::stoi(len) + 2 * LineBreakSep.size();
        if (package.size() < total)
        {
            std::cout<<total<<std::endl;
            return false;
        }

        // 如果走到这，说明一定有一个完整的报文
        *message = package.substr(pos + LineBreakSep.size(), std::stoi(len));
        package.erase(0, total);
        std::cout<<"完整报文得到\n";
        return true;
    }

    class Requst
    {
    public:
        Requst() : _data_x(0), _data_y(0), _oper(0)
        {
        }
        Requst(int x, int y, char op)
            : _data_x(x), _data_y(y), _oper(op)
        {
        }

        void Debug()
        {
            std::cout << "_data_x: " << _data_x << std::endl;
            std::cout << "_data_y: " << _data_y << std::endl;
            std::cout << "_oper: " << _oper << std::endl;
        }

        void Inc()
        {
            _data_x++;
            _data_y++;
        }

        bool Serializer(std::string *out)
        {
            // #ifdef SelfDefine
            *out = std::to_string(_data_x) + ProtSep + _oper + ProtSep + std::to_string(_data_y);
            return true;
            // #else

            // #endif
        }
        bool Deserializer(std::string &in)
        {
            auto left = in.find(ProtSep);
            if (left == std::string::npos)
                return false;
            auto right = in.rfind(ProtSep);
            if (right == std::string::npos)
                return false;

            _data_x = std::stoi(in.substr(0, left));
            _data_y = std::stoi(in.substr(right + ProtSep.size()));
            std::string oper = in.substr(left + ProtSep.size(), right - (left + ProtSep.size()));
            if (oper.size() != 1)
            {
                return false;
            }
            _oper = oper[0];
            return true;
        }

        int GetX()
        {
            return _data_x;
        }
        int GetY()
        {

            return _data_y;
        }
        char GetOper()
        {
            return _oper;
        }

    private:
        int _data_x;
        int _data_y;
        char _oper;
    };

    class Response
    {
    public:
        Response() : _result(0), _code(0)
        {
        }
        Response(int result, int code) : _result(result), _code(code)
        {
        }
        bool Serializer(std::string *out)
        {
            *out = std::to_string(_result) + ProtSep + std::to_string(_code);
            return true;
        }

        bool Deserializer(std::string &in)
        {

            auto pos = in.find(ProtSep);
            if (pos == std::string::npos)
                return false;
            _result = std::stoi(in.substr(0, pos));
            _code = std::stoi(in.substr(pos + ProtSep.size()));
            return true;
        }
        void SetResult(int res) { _result = res; }
        void SetCode(int code) { _code = code; }
        int GetResult() { return _result; }
        int GetCode() { return _code; }

    private:
        // "len\n_result _code\n"
        int _result; // 运算结果
        int _code;   // 运算状态
    };

    // 简单的工厂模式，建造类设计模式
    class Factory
    {
    public:
        std::shared_ptr<Requst> BuildRequest()
        {
            std::shared_ptr<Requst> req = std::make_shared<Requst>();
            return req;
        }

        std::shared_ptr<Requst> BuildRequest(int x,int y,char op)
        {
            std::shared_ptr<Requst> req = std::make_shared<Requst>(x,y,op);
            return req;
        }
        std::shared_ptr<Response> BuildResponse()
        {
            std::shared_ptr<Response> resp = std::make_shared<Response>();
            return resp;
        }
        std::shared_ptr<Response> BuildResponse(int result, int code)
        {
            std::shared_ptr<Response> req = std::make_shared<Response>(result, code);
            return req;
        }
    };

}
